{
 "cells": [
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-23T06:10:02.244152Z",
     "start_time": "2025-06-23T06:10:02.237967Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 04 数据操作\n",
    "import torch"
   ],
   "id": "70f92375d0c3768f",
   "outputs": [],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-23T06:11:36.390113Z",
     "start_time": "2025-06-23T06:11:36.380929Z"
    }
   },
   "cell_type": "code",
   "source": [
    "x = torch.arange(12)\n",
    "x"
   ],
   "id": "75fb84d1e202d35",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-23T06:11:37.948633Z",
     "start_time": "2025-06-23T06:11:37.933578Z"
    }
   },
   "cell_type": "code",
   "source": "x.shape",
   "id": "e03ef440c44e2c59",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([12])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-23T06:35:20.742775Z",
     "start_time": "2025-06-23T06:35:20.733295Z"
    }
   },
   "cell_type": "code",
   "source": "x.numel()",
   "id": "e0a17678222aa6cd",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-23T06:35:21.501051Z",
     "start_time": "2025-06-23T06:35:21.489733Z"
    }
   },
   "cell_type": "code",
   "source": [
    "X = x.reshape(3, 4)\n",
    "print(X)"
   ],
   "id": "940e9c855a508afd",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[ 0,  1,  2,  3],\n",
      "        [ 4,  5,  6,  7],\n",
      "        [ 8,  9, 10, 11]])\n"
     ]
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-23T06:37:12.085442Z",
     "start_time": "2025-06-23T06:37:12.076309Z"
    }
   },
   "cell_type": "code",
   "source": "torch.zeros((2,3,4))",
   "id": "e8756362533892de",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[0., 0., 0., 0.],\n",
       "         [0., 0., 0., 0.],\n",
       "         [0., 0., 0., 0.]],\n",
       "\n",
       "        [[0., 0., 0., 0.],\n",
       "         [0., 0., 0., 0.],\n",
       "         [0., 0., 0., 0.]]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 21
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-23T06:39:47.280905Z",
     "start_time": "2025-06-23T06:39:47.273337Z"
    }
   },
   "cell_type": "code",
   "source": "torch.ones((2,3,4))",
   "id": "a50853fdd293353d",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[1., 1., 1., 1.],\n",
       "         [1., 1., 1., 1.],\n",
       "         [1., 1., 1., 1.]],\n",
       "\n",
       "        [[1., 1., 1., 1.],\n",
       "         [1., 1., 1., 1.],\n",
       "         [1., 1., 1., 1.]]])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 30
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-23T06:39:55.432915Z",
     "start_time": "2025-06-23T06:39:55.416491Z"
    }
   },
   "cell_type": "code",
   "source": "torch.randn((3,4,5))",
   "id": "9ebbc43672e2c311",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[-1.1343,  0.9261,  1.0457, -1.3957,  1.2090],\n",
       "         [-0.3700,  1.3603, -0.2969,  2.7347, -0.4019],\n",
       "         [-0.1454, -0.3818,  0.7452, -1.0138, -0.2684],\n",
       "         [-0.1365,  1.9526, -1.5434,  2.9913,  0.5658]],\n",
       "\n",
       "        [[-1.4571,  0.0940,  0.0703,  1.5204, -0.1079],\n",
       "         [ 0.2429,  1.7240,  0.6345,  0.4107,  1.7329],\n",
       "         [-1.3856, -0.8849, -0.2633, -1.4846,  0.1475],\n",
       "         [-0.6314,  0.6323,  0.8242,  0.4471,  1.5962]],\n",
       "\n",
       "        [[ 0.6619, -0.5394, -1.0124,  0.5518, -1.8147],\n",
       "         [ 0.8860,  1.8373,  1.7049,  1.0913,  2.2873],\n",
       "         [ 0.2840, -0.3650, -1.4283,  0.3242, -2.4570],\n",
       "         [ 0.5978, -0.5097,  0.6679,  0.3160, -1.6048]]])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 32
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-23T06:40:39.473839Z",
     "start_time": "2025-06-23T06:40:39.462913Z"
    }
   },
   "cell_type": "code",
   "source": "torch.tensor([[3,4,5],[6,7,8],[1,3,5,]])",
   "id": "7381ac8fee5d9e41",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[3, 4, 5],\n",
       "        [6, 7, 8],\n",
       "        [1, 3, 5]])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 35
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-23T06:37:32.611740Z",
     "start_time": "2025-06-23T06:37:32.606632Z"
    }
   },
   "cell_type": "code",
   "source": "print(torch.tensor([[2,1,4,3],[1,2,3,4],[4,3,2,1]]).shape)",
   "id": "c1ed5c303ed32789",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([3, 4])\n"
     ]
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-23T06:37:43.766179Z",
     "start_time": "2025-06-23T06:37:43.760283Z"
    }
   },
   "cell_type": "code",
   "source": [
    "x = torch.tensor([1.0,2,4,8])\n",
    "y = torch.tensor([2,2,2,2])\n",
    "x+y,x-y,x*y,x/y,x**y"
   ],
   "id": "1720cce0a05bd5e1",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([ 3.,  4.,  6., 10.]),\n",
       " tensor([-1.,  0.,  2.,  6.]),\n",
       " tensor([ 2.,  4.,  8., 16.]),\n",
       " tensor([0.5000, 1.0000, 2.0000, 4.0000]),\n",
       " tensor([ 1.,  4., 16., 64.]))"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 24
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-23T06:37:45.907764Z",
     "start_time": "2025-06-23T06:37:45.889921Z"
    }
   },
   "cell_type": "code",
   "source": "torch.exp(x)",
   "id": "dd7b57f911a5cb4a",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([2.7183e+00, 7.3891e+00, 5.4598e+01, 2.9810e+03])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 25
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-23T06:36:04.211539Z",
     "start_time": "2025-06-23T06:36:04.191576Z"
    }
   },
   "cell_type": "code",
   "source": [
    "X = torch.arange(12,dtype=torch.float32).reshape(3,4)\n",
    "Y = torch.tensor([[2.0,1,4,3],[1,2,3,4],[4,3,2,1]])\n",
    "torch.cat((X,Y),dim=0), torch.cat((X,Y),dim=1),torch.cat((X,Y))"
   ],
   "id": "ce6a45f1ff6d59d",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[ 0.,  1.,  2.,  3.],\n",
       "         [ 4.,  5.,  6.,  7.],\n",
       "         [ 8.,  9., 10., 11.],\n",
       "         [ 2.,  1.,  4.,  3.],\n",
       "         [ 1.,  2.,  3.,  4.],\n",
       "         [ 4.,  3.,  2.,  1.]]),\n",
       " tensor([[ 0.,  1.,  2.,  3.,  2.,  1.,  4.,  3.],\n",
       "         [ 4.,  5.,  6.,  7.,  1.,  2.,  3.,  4.],\n",
       "         [ 8.,  9., 10., 11.,  4.,  3.,  2.,  1.]]),\n",
       " tensor([[ 0.,  1.,  2.,  3.],\n",
       "         [ 4.,  5.,  6.,  7.],\n",
       "         [ 8.,  9., 10., 11.],\n",
       "         [ 2.,  1.,  4.,  3.],\n",
       "         [ 1.,  2.,  3.,  4.],\n",
       "         [ 4.,  3.,  2.,  1.]]))"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 18
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-23T06:43:51.428714Z",
     "start_time": "2025-06-23T06:43:51.408348Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "X,Y,print(X==Y)"
   ],
   "id": "cb244f6d0352ab76",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[False,  True, False,  True],\n",
      "        [False, False, False, False],\n",
      "        [False, False, False, False]])\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(tensor([[ 0.,  1.,  2.,  3.],\n",
       "         [ 4.,  5.,  6.,  7.],\n",
       "         [ 8.,  9., 10., 11.]]),\n",
       " tensor([[2., 1., 4., 3.],\n",
       "         [1., 2., 3., 4.],\n",
       "         [4., 3., 2., 1.]]),\n",
       " None)"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 38
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-23T06:44:06.458519Z",
     "start_time": "2025-06-23T06:44:06.448476Z"
    }
   },
   "cell_type": "code",
   "source": "X.sum()",
   "id": "b170e29a19c7db73",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(66.)"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 39
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-23T06:44:13.521679Z",
     "start_time": "2025-06-23T06:44:13.513812Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 广播机制,说白了就是补0操作\n",
    "a = torch.arange(3).reshape((3, 1))\n",
    "b = torch.arange(2).reshape((1, 2))\n",
    "a, b"
   ],
   "id": "61c8a0c1004924cf",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[0],\n",
       "         [1],\n",
       "         [2]]),\n",
       " tensor([[0, 1]]))"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 40
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-05T13:59:31.619212Z",
     "start_time": "2025-05-05T13:59:31.605075Z"
    }
   },
   "cell_type": "code",
   "source": "print(a+b)",
   "id": "28c4f7198a3e84b2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[0, 1],\n",
      "        [1, 2],\n",
      "        [2, 3]])\n"
     ]
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-06-23T06:46:38.092523Z",
     "start_time": "2025-06-23T06:46:38.082959Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(X)\n",
    "X[-2], X[-1], X[1:3]"
   ],
   "id": "22e2032e88a60e56",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[ 0.,  1.,  2.,  3.],\n",
      "        [ 4.,  5.,  6.,  7.],\n",
      "        [ 8.,  9., 10., 11.]])\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(tensor([4., 5., 6., 7.]),\n",
       " tensor([ 8.,  9., 10., 11.]),\n",
       " tensor([[ 4.,  5.,  6.,  7.],\n",
       "         [ 8.,  9., 10., 11.]]))"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 43
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-05T13:59:31.681540Z",
     "start_time": "2025-05-05T13:59:31.666814Z"
    }
   },
   "cell_type": "code",
   "source": [
    "X[1, 2] = 9 #索引第二行第三列\n",
    "print(X)"
   ],
   "id": "f6ec40a3e0d07b4c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[ 0.,  1.,  2.,  3.],\n",
      "        [ 4.,  5.,  9.,  7.],\n",
      "        [ 8.,  9., 10., 11.]])\n"
     ]
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-05T13:59:31.728290Z",
     "start_time": "2025-05-05T13:59:31.714728Z"
    }
   },
   "cell_type": "code",
   "source": [
    "X[0:2, :] = 12\n",
    "print(X)"
   ],
   "id": "b39a1be9a79326ac",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[12., 12., 12., 12.],\n",
      "        [12., 12., 12., 12.],\n",
      "        [ 8.,  9., 10., 11.]])\n"
     ]
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-05T13:59:31.759438Z",
     "start_time": "2025-05-05T13:59:31.745214Z"
    }
   },
   "cell_type": "code",
   "source": [
    "before = id(Y) #引用地址问题\n",
    "Y = Y + X\n",
    "id(Y) == before"
   ],
   "id": "e1ba1d3decfdac44",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 18
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-05T13:59:31.789337Z",
     "start_time": "2025-05-05T13:59:31.775582Z"
    }
   },
   "cell_type": "code",
   "source": [
    "Z = torch.zeros_like(Y)#新做一个全0张量\n",
    "print('id(Z):', id(Z))\n",
    "Z[:] = X + Y#原地操作\n",
    "print('id(Z):', id(Z))"
   ],
   "id": "dd0087138882040a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "id(Z): 1147330784\n",
      "id(Z): 1147330784\n"
     ]
    }
   ],
   "execution_count": 19
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-05T13:59:31.819790Z",
     "start_time": "2025-05-05T13:59:31.805523Z"
    }
   },
   "cell_type": "code",
   "source": [
    "before = id(X)\n",
    "X += Y#也可以原地操作\n",
    "id(X) == before"
   ],
   "id": "6f9ff712e6045ca0",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 20
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-05T13:59:31.865485Z",
     "start_time": "2025-05-05T13:59:31.851777Z"
    }
   },
   "cell_type": "code",
   "source": [
    "A = X.numpy()\n",
    "B = torch.tensor(A)\n",
    "print(type(A)), print(type(B))\n",
    "A, B"
   ],
   "id": "fb07c665e654a175",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'numpy.ndarray'>\n",
      "<class 'torch.Tensor'>\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(array([[26., 25., 28., 27.],\n",
       "        [25., 26., 27., 28.],\n",
       "        [20., 21., 22., 23.]], dtype=float32),\n",
       " tensor([[26., 25., 28., 27.],\n",
       "         [25., 26., 27., 28.],\n",
       "         [20., 21., 22., 23.]]))"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 21
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-05T13:59:31.910590Z",
     "start_time": "2025-05-05T13:59:31.896877Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = torch.tensor([3.5])\n",
    "a, a.item(), float(a), int(a)"
   ],
   "id": "89959cde3a055d76",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([3.5000]), 3.5, 3.5, 3)"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-05T13:59:31.957517Z",
     "start_time": "2025-05-05T13:59:31.943545Z"
    }
   },
   "cell_type": "code",
   "source": "",
   "id": "b1024ef1d5b4aae8",
   "outputs": [],
   "execution_count": null
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
